using System;
using System.Linq;

namespace DynamicConfigurationManager.ConfigMapAttributes
{
    internal class CommandLineArgs : IConfigMapAttribute
    {
        // handle commandLineArg="env=GUS-INIT-SIT"
        public bool Execute( string configMapAttribute )
        {
            // Make sure config attribute uses "var=value" syntax
            var argandvalue = configMapAttribute.Split( "=".ToCharArray(), 2, StringSplitOptions.RemoveEmptyEntries );
            if( argandvalue.Length != 2 )
            {
                throw new ArgumentException( "Incorrect syntax for commandLineArg match: " + configMapAttribute );
            }

            return ( from arg in Environment.GetCommandLineArgs()
                     let varval = arg.Split( "=".ToCharArray(), 2, StringSplitOptions.RemoveEmptyEntries )
                     where varval.Length == 2
                     select arg )
                    .Any( arg => arg.Equals( configMapAttribute, StringComparison.InvariantCultureIgnoreCase ) );
        }
    }
}